<?php
/**
* Request Manage
* @author trungdt 
*/
	if (!defined('EXPONENT')) exit('');
	
	global $db;
	
	$config = $db->selectObject('requestmodule_config',"location_data='".serialize($loc)."'");
	
	if (exponent_users_isLoggedIn() && $config)
	{
		exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);
		
		global $user;
		$sql_where=" (user_id = {$user->id}) ";
		if (exponent_users_isAdmin() || 
			exponent_permissions_check(array("edit","delete","administrate", "approve", "add_calendar"),$loc))
		{
			$sql_where=" 1 ";
		}
		
		/*
		// get page
		$page=1;
		if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
		if (($page<1) || ($page>$page_count)) $page=1;
		$start_pos=($page-1)*$max_item;
		
		$sql="SELECT {$db->prefix}rsrequest_item.*, {$db->prefix}user.username, {$db->prefix}user.firstname, {$db->prefix}user.lastname 
		FROM {$db->prefix}rsrequest_item LEFT JOIN {$db->prefix}user
		ON {$db->prefix}rsrequest_item.user_id = {$db->prefix}user.id
		WHERE {$sql_where} AND (request_type = 0)
		ORDER BY request_date DESC, admin_unread DESC, unread DESC
		LIMIT {$start_pos},{$max_item}
		";
		$data=$db->selectObjectsBySql($sql);
		
		foreach ($data as $k=>$v) {
			// 
			$v->userfullname=trim("{$v->firstname} {$v->lastname}");
			$v->request_date=rsrequestmodule::_dateFormat($v->request_date);
			
			// search for all respones for each request
			$sql="SELECT {$db->prefix}rsrequest_item.*, {$db->prefix}user.username, {$db->prefix}user.firstname, {$db->prefix}user.lastname 
			FROM {$db->prefix}rsrequest_item LEFT JOIN {$db->prefix}user
			ON {$db->prefix}rsrequest_item.user_id = {$db->prefix}user.id
			WHERE (request_type = 1) AND ({$db->prefix}rsrequest_item.parent_id = {$v->id})
			ORDER BY request_date DESC, admin_unread DESC, unread DESC
		";
			$temp=$db->selectObjectsBySql($sql);
			
			foreach ($temp as $k1=>$v1){
				if ($v1->unread || 
				((exponent_users_isAdmin() || exponent_permissions_check(array("edit","delete","administrate"),$loc)) && ($v1->admin_unread)))
				{
					$v->un_read=1;
					$v->admin_unread=1;
					break;
				}
			}
			//  date format for all respone
			foreach ($temp as $k1=>$v1){
				$v1->userfullname=trim("{$v1->firstname} {$v1->lastname}");
				$v1->request_date=rsrequestmodule::_dateFormat($v1->request_date, 0);
			}
			$v->respones=$temp;
		}
		
		$template = new template('rsrequestmodule','_request_manage',$loc);
		$template->assign('requests',$data);
		$template->assign('start_index',$start_pos+1);
		$template->assign('page',$page);
		$template->assign('pages',$page_count);
		if (exponent_users_isAdmin() || 
			exponent_permissions_check(array("edit","delete","administrate"),$loc))
			{
				$template->assign('admin_mode',1);
			}
			else
			{
				$template->assign('admin_mode',0);
			}
		$template->register_permissions(
		array('administrate','configure','edit','delete'),$loc);

		$template->output();*/
	}
	else
	{
		echo SITE_404_HTML;
	}
?>